
/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  if (!search) {
    return {}
  }
  const obj = {}
  const searchArr = search.split('&')
  searchArr.forEach(v => {
    const index = v.indexOf('=')
    if (index !== -1) {
      const name = v.substring(0, index)
      const val = v.substring(index + 1, v.length)
      obj[name] = val
    }
  })
  return obj
}

export function handleColumns(data, props) {
  const options = []
  data.forEach(item => {
    for (const i in props) {
      item[i] = item[props[i]]
    }
    options.push(item)
  })
  return options
}

// 判断PC移动，浏览器内核等...
export function judgeBrowser() {
  const userAgent = window.navigator.userAgent
  const obj = {
    isFromAndroid: /android/gi.test(userAgent), /* 安卓*/
    isFromIOs: /iphone|ipod|ios/gi.test(userAgent), /* IOS */
    isFromWeChat: /MicroMessenger/gi.test(userAgent), /* 微信 */
    isFromQQ: /mobile.*qq/gi.test(userAgent), /* QQ */
    isFromUC: /ucbrowser/gi.test(userAgent), /* UC浏览器 */
    isFromQQBrowser: /mqqbrowser[^LightApp]/gi.test(userAgent), /* QQ浏览器 */
    isFromQQBrowserLight: /MQQBrowserLightApp/gi.test(userAgent), /* qq轻浏览器*/

    isTrident: /Trident/gi.test(userAgent), /** IE内核 */
    isPresto: /Presto/gi.test(userAgent), /** opera内核 */
    iswebKit: /AppleWebKit/gi.test(userAgent), /** 苹果、谷歌内核 */
    isGecko: /Gecko.*KHTML/gi.test(userAgent), /** 火狐内核 */

    isMobile: !!userAgent.match(/AppleWebKit.*Mobile.*/), /** 是否为移动终端 */
    isIos: !!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), /* ios终端 */
    isAndroid: userAgent.indexOf('Android') > -1 || userAgent.indexOf('Adr') > -1, // android终端
    isIPhone: userAgent.indexOf('iPhone') > -1, // iPhone或者QQHD浏览器
    isIPad: userAgent.indexOf('iPad') > -1, // iPad
    isWebApp: userAgent.indexOf('Safari') === -1 // web应该程序，没有头部与底部
  }
  return obj
}

// 判断是不是X类型手机
export function isIphonex() {
  // X XS, XS Max, XR，11， 11pro，11pro max，12mini，12， 12 pro，12 pro max
  const xSeriesConfig = [
    {
      devicePixelRatio: 3,
      width: 375,
      height: 812
    },
    {
      devicePixelRatio: 3,
      width: 414,
      height: 896
    },
    {
      devicePixelRatio: 2,
      width: 414,
      height: 896
    },
    {
      devicePixelRatio: 3,
      width: 315,
      height: 812
    },
    {
      devicePixelRatio: 3,
      width: 390,
      height: 844
    },
    {
      devicePixelRatio: 3,
      width: 428,
      height: 926
    }
  ]
  // h5
  if (typeof window !== 'undefined' && window) {
    const isIOS = /iphone/gi.test(window.navigator.userAgent)
    if (!isIOS) return false
    const { devicePixelRatio, screen } = window
    const { width, height } = screen
    return xSeriesConfig.some(item => item.devicePixelRatio === devicePixelRatio && item.width === width && item.height === height)
  }
  return false
}
